﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rabbit.Logic;
using RabbitShop.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitShop.Action
{
    public partial class OrderOneShop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }


            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(postData);
            string postContent = sRead.ReadToEnd();
            sRead.Close();

            if (!string.IsNullOrEmpty(postContent))
            {
                try
                {
                    ShopItem item = JsonConvert.DeserializeObject<ShopItem>(postContent);
                    
                    Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
                    customer.PreOrderShopItem = item;
                    if (customer.ShopList == null)
                    {
                        customer.ShopList = new List<ShopItem>();
                        customer.ShopList.Add(item);
                    }
                    else
                    {
                        ShopItem targetShopItem = customer.ShopList.FirstOrDefault(x => x.ShopId == item.ShopId);
                        if (targetShopItem == null)
                        {
                            customer.ShopList.Add(item);
                        }
                        else
                        {
                            foreach (ShopCartItem cartItem in item.CartItemList)
                            {
                                ShopCartItem shopCartItem = targetShopItem.CartItemList.FirstOrDefault(x => x.ProductId == cartItem.ProductId);
                                if (shopCartItem == null)
                                {
                                    targetShopItem.CartItemList.Add(cartItem);
                                }
                                else
                                {
                                    shopCartItem.Quantity = cartItem.Quantity;
                                }
                            }
                        }
                    }
                    RabbitUse.AddOrUpdateCustomer(Db.Store, customer);

                    Logic.UpdateCookieCartNo(this, customer.ShopList.Count.ToString());

                    Response.Write("true");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }

            }
            else
            {
                Response.Write("false");
            }
        }
    }
}